﻿using System;
using System.Linq;
using Collab.Modules.Session;
using Collab.Services.Remote.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Collab.Test
{


    /// <summary>
    ///This is a test class for SolutionTrackerTest and is intended
    ///to contain all SolutionTrackerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class SolutionTrackerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for InsertProjectItemToSolution
        ///</summary>
        [TestMethod()]
        [Ignore]
        [DeploymentItem("Collab.Modules.Session.dll")]
        public void InsertProjectItemToSolutionTest()
        {
            var target = SetupSolutionStructure();
            ProjectItem projectItem = new ProjectItem { Name = "Class3", Parents = new string[] { "Project1", "Folder1" } };
            target.InsertProjectItemToSolution(projectItem);
            Assert.IsNotNull(target.solution);
            Assert.AreEqual(1, target.solution.Children.Count);
            Assert.AreEqual(2, target.solution.Children[0].Children.Count);
            Assert.AreEqual(2, target.solution.Children[0].Children.First(child => child.Name == "Folder2").Children.Count);
            Assert.AreEqual(3, target.solution.Children[0].Children.First(child => child.Name == "Folder1").Children.Count);
            Assert.IsTrue(target.solution.Children[0].Children.First(child => child.Name == "Folder1").Children.Any(child => child.Name == "Class3"),
                "Class3 was not added properly");
        }

        /// <summary>
        ///A test for RemoveProjectItemFromSolution
        ///</summary>
        [TestMethod()]
        [Ignore]
        [DeploymentItem("Collab.Modules.Session.dll")]
        public void RemoveProjectItemFromSolutionTest()
        {
            var target = SetupSolutionStructure();
            ProjectItem projectItem = new ProjectItem { Name = "Class2", Parents = new string[] { "Project1", "Folder1" } };
            target.RemoveProjectItemFromSolution(projectItem);

            Assert.IsNotNull(target.solution);
            Assert.AreEqual(1, target.solution.Children.Count);
            Assert.AreEqual(2, target.solution.Children[0].Children.Count);
            Assert.AreEqual(2, target.solution.Children[0].Children.First(child => child.Name == "Folder2").Children.Count);
            Assert.AreEqual(1, target.solution.Children[0].Children.First(child => child.Name == "Folder1").Children.Count);
            Assert.IsTrue(target.solution.Children[0].Children.First(child => child.Name == "Folder1").Children.Any(child => child.Name == "Class1"),
                "Class1 was removed when it shouldn't have been");
            Assert.IsTrue(!target.solution.Children[0].Children.First(child => child.Name == "Folder1").Children.Any(child => child.Name == "Class2"),
                "Class2 was NOT removed when it should have been");
        }

        protected SolutionTracker_Accessor SetupSolutionStructure()
        {
            PrivateObject param0 = new PrivateObject(new EnvDTE.DTE());
            SolutionTracker_Accessor target = new SolutionTracker_Accessor(param0);
            target.solution = new Solution
            {
                Name = "TestSol",
                Children = new System.Collections.Generic.List<ProjectItem>
                {
                    new ProjectItem{Name = "Project1", 
                        Children = new System.Collections.Generic.List<ProjectItem>
                        {
                             new ProjectItem{Name = "Folder1", 
                                  Children = new System.Collections.Generic.List<ProjectItem>
                                  {
                                      new ProjectItem{Name = "Class1", },
                                      new ProjectItem{Name = "Class2", },
                                  }
                             },
                             new ProjectItem{Name = "Folder2", 
                                  Children = new System.Collections.Generic.List<ProjectItem>
                                  {
                                      new ProjectItem{Name = "Class3", },
                                      new ProjectItem{Name = "Class4", },
                                  }
                             },
                        }
                    }
                }
            };

            return target;

        }
    }
}
